﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

namespace uHelpsy.Core.Helpers
{
    /// <summary>
    /// Helper methods for image manipulation.
    /// </summary>
    public class Drawing
    {
        #region CreateAndSaveThumbnail

        public static void CreateAndSaveThumbnail(string folderPath, string fileName, byte[] bytes, int width, int height)
        {
            using (MemoryStream m = new MemoryStream(bytes))
            {
                Bitmap b = new Bitmap(m);
                Image thumbnail = CreateThumbnailImage(b, width, height);
                thumbnail.Save(folderPath + fileName);
                b.Dispose();
                thumbnail.Dispose();
                m.Close();
            }
        }

        #endregion


        #region CreateThumbnailImage

        /// <summary>
        /// Creates a new thumbnail image retaining the aspect ratio of the original image.
        /// </summary>
        /// <param name="data">The image</param>
        /// <param name="maxWidth">The maximum width of the thumbnail</param>
        /// <param name="maxHeight">The maximum height of the thumbnail</param>
        /// <returns>The thumbnail image</returns>
        public static Image CreateThumbnailImage(Image image, int maxWidth, int maxHeight)
        {
            if (image.Width <= maxWidth && image.Height <= maxHeight)
            {
                // clone the image to prevent sharing issues
                return (Bitmap)image.Clone();
            }

            // calculate correct dimensions
            double scale;
            if (image.Width * maxHeight > image.Height * maxWidth)
                scale = maxWidth / (double)image.Width;
            else
                scale = maxHeight / (double)image.Height;

            int newWidth = (int)(image.Width * scale);
            int newHeight = (int)(image.Height * scale);

            // create the thumbnail
            Bitmap thumb = new Bitmap(newWidth, newHeight);

            using (Graphics graphics = Graphics.FromImage(thumb))
            {
                graphics.InterpolationMode = InterpolationMode.Default;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.DrawImage(image, 0, 0, thumb.Width, thumb.Height);
                return thumb;
            }
        }

        #endregion


        #region GetImageOrDefault

        /// <summary>
        /// Returns an image or null.
        /// </summary>
        /// <param name="filename">The full file path</param>
        /// <returns></returns>
        public static Image GetImageOrDefault(string filename)
        {
            Image image;
            try
            {
                image = Image.FromFile(filename);
            }
            catch (NotSupportedException ex)
            {
                // returns null when it was not an image!
                return null;
            }
            return image;
        }

        #endregion


        #region IsImage

        /// <summary>
        /// Returns true if file is an image.
        /// </summary>
        /// <param name="filename">The full file path</param>
        /// <returns></returns>
        public static bool IsImage(string filename)
        {
            if (GetImageOrDefault(filename) == null)
                return false;

            return true;
        }

        #endregion


    }
}
